function y_cos = coswin(y_data, end_pts);

% COSWIN - cosine window data for FFTs
%
%    YW = COSWIN(Y, P) creates a cosine window to attenuate
%    the endpoints of Y for use in FFTs.  The percentage
%    of points attenuated by the window is specified by P:
%
%             P/2           1 - P           P/2
%          |-------|---------------------|-------|
%
%                  ***********************
%               *                           *
%             *                               *
%            *                                 *
%           *                                   *
%          *                                     *

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: August 9, 2000

% initialize output

y_cos = [];

% validate input arguments

if (nargin ~= 2)
   msgbox('Invalid number of arguments', 'COSWIN Error', 'warn');
   return
elseif (~isnumeric(y_data) | isempty(y_data))
   msgbox('Y must be a numeric array', 'COSWIN Error', 'warn');
   return
elseif (~isscalar(end_pts))
   msgbox('N must be a scalar', 'COSWIN Error', 'warn');
   return
elseif (end_pts < 0)
   end_pts = 0;
elseif (end_pts > 100)
   end_pts = 100;
end

% create cosine window
% multiply data by window

num_pts = ceil(end_pts * size(y_data, 2) / 200);
y_end = cos(pi * (1 : num_pts) / (2 * num_pts));
y_beg = fliplr(y_end);
y_cos = ones(size(y_data));
for i = 1 : size(y_cos, 1)
   y_cos(i, 1 : num_pts) = y_beg;
   y_cos(i, end - num_pts + 1 : end) = y_end;
end
y_cos = y_data .* y_cos;
return
